package com.cloudcc.mobile.view.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.MenuToggleEventR;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrClassicDefaultHeader;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrClassicFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrDefaultHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrHandler;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.PushSaveMessage;
import com.cloudcc.mobile.event.BeauEventList;
import com.cloudcc.mobile.messagecent.BeauTongzhi;
import com.cloudcc.mobile.messagecent.TongzhiImpl;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

public class BaseSlidingRightActivity extends BaseActivity implements PtrHandler {
    public SlidingMenu menu_R;
    //下拉刷新
   public PtrClassicFrameLayout xlsx_x_bsr;
    public ListView xmListview;
    public List xx = new ArrayList();
    public SlidingRightAdapter tzadapter_x;
    private TongzhiImpl tongzhiImpl = new TongzhiImpl();
    private BeauEventList.BeauTongzhiEvent event;
    protected List<PushSaveMessage> messsageList;
    @Override
    public void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        initMessage();
        initMenu();

        initRefreshTz();
    }

    public void initMenu() {
        menu_R = new SlidingMenu(this);
        menu_R.setMode(SlidingMenu.RIGHT);
        menu_R.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); // 触摸边界拖出菜单
//        menu.setShadowDrawable(R.drawable.shadow);//设置阴影图片												// 取消滑动SlidingMenu.TOUCHMODE_NONE,打开滑动SlidingMenu.TOUCHMODE_FULLSCREEN
//		menu.setShadowWidthRes(R.dimen.shadow_width);
        menu_R.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu_R.setOffsetFadeDegree(0.4f);
        menu_R.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        View viewRi = LayoutInflater.from(this).inflate(R.layout.activity_base_sliding_right,
                null);
        menu_R.setMenu(viewRi);
        xlsx_x_bsr = (PtrClassicFrameLayout)viewRi.findViewById(R.id.xlsx_x_bsr);
        xmListview = (ListView)viewRi.findViewById(R.id.messagee_lv_x);

    }
    public void onEventMainThread(MenuToggleEventR event) {
        if (menu_R == null) {
            return;
        }
        if (event.isToggle) {
            if (event.open) {
            }
        } else {
            menu_R.toggle();
            initRequestDataTz();
        }
    }
    public void initRequestDataTz() {
        xlsx_x_bsr.postDelayed(new Runnable() {
            @Override
            public void run() {
                xlsx_x_bsr.autoRefresh(true);
            }
        }, 150);
    }

    private void initRefreshTz() {
        xlsx_x_bsr.setLoadingMinTime(1000);
        xlsx_x_bsr.setPtrHandler(this);
        PtrClassicDefaultHeader header = new PtrClassicDefaultHeader(mContext);
        header.setbg();
        xlsx_x_bsr.setHeaderView(header);
        xlsx_x_bsr.addPtrUIHandler(header);

    }


    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
       return PtrDefaultHandler.checkContentCanBePulledDown(frame,xmListview,header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        initMessage();
//        tzadapter_x = new SlidingRightAdapter(messsageList,this);
//        xmListview.setAdapter(tzadapter_x);
//        xlsx_x_bsr.refreshComplete();
    }
    private void initMessage(){
//        PushSaveMessageDB db = new PushSaveMessageDB(this, UserManager
//                .getManager().getUser().userId + "pushsavemessage");
//        try {
//            messsageList = db.findAllPushSaveMessage(PushSaveMessage.class);
//            if(messsageList==null) {
//                messsageList = new ArrayList<>();
//            }else{
//                Collections.reverse(messsageList);
//            }
//        } catch (DbException e) {
//            e.printStackTrace();
//        }

        event = new BeauEventList.BeauTongzhiEvent();
        tongzhiImpl.tongzhiDate(event);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(menu_R.isMenuShowing()){
            EventEngine.post(new MenuToggleEventR(false, false));
        }
    }
    public void onEventMainThread(BeauEventList.BeauTongzhiEvent event) {
        if (event.isError()) {
            return;
        }
        List<BeauTongzhi> tongzhiLi = event.getData();
        tzadapter_x = new SlidingRightAdapter(tongzhiLi,mContext);
        xmListview.setAdapter(tzadapter_x);
        xlsx_x_bsr.refreshComplete();

    }
}
